package com.example.sspulibrary.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.sspulibrary.R;
import com.example.sspulibrary.adapter.SelectBookAdapter;
import com.example.sspulibrary.db.DBDefinitionManipulation;
import com.example.sspulibrary.model.ExceptBook;

import java.util.List;

public class SaveFragment extends Fragment {
    ListView listView;
    DBDefinitionManipulation db;
    SelectBookAdapter bookAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.save_fragment, container, false);
        initView(view);
        addBooksToList(getBookInfo());

        return view;
    }

    private void addBooksToList(List<ExceptBook> exceptBooks) {
        // 设置适配器
        bookAdapter = new SelectBookAdapter(getContext(), exceptBooks);
        listView.setAdapter(bookAdapter);
        Toast.makeText(getActivity(), "显示完成", Toast.LENGTH_SHORT).show();
    }

    private void initView(View view) {
        listView = view.findViewById(R.id.admin_select_lv);
    }

    private List<ExceptBook> getBookInfo() {
        db = new DBDefinitionManipulation(getContext());
        db.open();
        List<ExceptBook> exceptBooks = db.queryExceptBooks();
        db.close();
        return exceptBooks;
    }
}
